package com.lzh.o2o.web.frontend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzh.o2o.dto.ProductExecution;
import com.lzh.o2o.pojo.Product;
import com.lzh.o2o.pojo.ProductCategory;
import com.lzh.o2o.pojo.Shop;
import com.lzh.o2o.service.ProductCategoryService;
import com.lzh.o2o.service.ProductService;
import com.lzh.o2o.service.ShopService;
import com.lzh.o2o.util.HttpServletRequestUtil;

/** 
* @author  haohao: 
* @date 创建时间：2018年9月24日 下午8:16:07 
* @version 1.0 
* @parameter 
* @since  
* @return  
*/
@Controller
@RequestMapping("/frontend")
public class ShopDetailController {

	@Autowired
	private ShopService shopService;
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	 * 
	* @Title: listShopDetailPageInfo 
	* @Description: 获取店铺信息以及该店铺下面的商品类别列表 
	* @param request
	* @return Map<String,Object>
	* @author haohao
	* @date 2018年9月24日下午8:24:22
	 */
	@RequestMapping(value="/listshopdetailpageinfo",method=RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> listShopDetailPageInfo(HttpServletRequest request){
		Map<String,Object>modelMap=new HashMap<String,Object>();
		//获取前台传过来的shopId
		Long shopId = HttpServletRequestUtil.getLong(request, "shopId");
		Shop shop=null;
		List<ProductCategory> productCategoryList=null;
		if(shopId!=-1){
			//获取店铺id为shopId的店铺信息
			 shop = shopService.getByShopId(shopId);
			 //获取店铺下面的商品类别列表
			 productCategoryList = productCategoryService.getProductCategoryList(shopId);
			 modelMap.put("shop", shop);
			 modelMap.put("productCategoryList", productCategoryList);
			 modelMap.put("success", true);
		}else{
			modelMap.put("success", false);
			modelMap.put("errMsg","empty shopId");
		}
		return modelMap;
	}
	
	/**
	 * 
	* @Title: listProductsByShop 
	* @Description: 依据查询条件分页列出该店铺下面的所有商品 
	* @param request
	* @return Map<String,Object>
	* @author haohao
	* @date 2018年9月24日下午8:41:48
	 */
	@RequestMapping(value="/listproductsbyshop",method=RequestMethod.GET)
	@ResponseBody
	private Map<String,Object>listProductsByShop(HttpServletRequest request){
		Map<String,Object> modelMap=new HashMap<String,Object>();
		//获取页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取一页需要显示的条数
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		//获取店铺id
		Long shopId = HttpServletRequestUtil.getLong(request, "shopId");
		//空值判断
		if((pageIndex>-1)&&(pageSize>-1)&&(shopId>-1)){
			//尝试获取商品类别id
			Long productCategoryId = HttpServletRequestUtil.getLong(request, "producntId");
			//尝试获取商品名称(模糊查询使用)
			String productName = HttpServletRequestUtil.getString(request, "productName");
			//组合查询条件
			Product productCondition=compactProductCondition4Search(shopId,productCategoryId,productName);
			//按照传入的查询条件以及分页信息返回相应商品列表以及总数
			ProductExecution pe = productService.getProductList(productCondition, pageIndex, pageSize);
			modelMap.put("productList", pe.getProductList());
			modelMap.put("count", pe.getCount());
			modelMap.put("success", true);
		}else{
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");
		}
		return modelMap;
		
	}
	//组合攒讯条件，封装到productCondition对象里返回
	private Product compactProductCondition4Search(Long shopId, Long productCategoryId, String productName) {
		Product productCondition = new Product();
		Shop shop = new Shop();
		shop.setShopId(shopId);
		productCondition.setShop(shop);
		
		if(productCategoryId!=-1L){
			ProductCategory productCategory=new ProductCategory();
			productCategory.setProductCategoryId(productCategoryId);
			productCondition.setProductCategory(productCategory);
		}
		if(productName!=null){
			productCondition.setProductName(productName);
		}
		//只允许选出状态为上架的商品
		productCondition.setEnableStatus(1);
		return productCondition;
	}
	
	
}



